package cc.linker.steplink.crm.merchanth5.service;


import cc.linker.steplink.crm.commons.oss.OssService;
import cc.linker.steplink.crm.merchanth5.dao.TreatyDao;
import cc.linker.steplink.crm.merchanth5.pojo.TreatyPo;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * 平台协议
 * @author huhj
 */

@Service
public class TreatyService {


    @Autowired
    private TreatyDao treatyDao;

    public List<TreatyPo> getList(String orgId) {
        TreatyPo treatyPo = new TreatyPo();
        treatyPo.setOrgId(orgId);
        List<TreatyPo> list = treatyDao.getList(treatyPo);
        if(null == list || list.size() == 0){
            list = this.initTreaty(orgId);
        }
        return list;
    }


    /**
     * 用户注册协议、隐私政策、联盟商家规则、供货商规则、旺旺直播规则、加盟代理规则
     * @param orgId
     * @return
     */
    private List<TreatyPo> initTreaty(String orgId) {
        TreatyPo treatyPo = new TreatyPo();
        treatyPo.setOrgId("00000");
        List<TreatyPo> list = treatyDao.getList(treatyPo);
        for(TreatyPo tpo:list){
            tpo.setTreatyId(UUID.randomUUID().toString());
            tpo.setOrgId(orgId);
            tpo.setUpdateTime(tpo.getCreateTime());
            tpo.setCreateTime(tpo.getUpdateTime());
            treatyDao.addTreaty(tpo);
        }
        return list;
    }


    public void edit(Map<String, Object> param) {
        TreatyPo treatyPo = new TreatyPo();
        treatyPo.setUpdateTime(new Date());
        treatyPo.setTreatyContent(param.get("treatyContent").toString());
        treatyPo.setTreatyId(param.get("treatyId").toString());
        treatyPo.setOrgId(param.get("orgId").toString());
        treatyDao.edit(treatyPo);
    }

    public void reSet(Map<String, Object> param) {
        String treatyName = param.get("treatyName").toString();
        String[] names = {"加盟代理规则","用户注册协议","供货商规则","联盟商家规则","旺旺直播规则","隐私政策"};
        List<String> asList = Arrays.asList(names);
        if(!asList.contains(treatyName)){
            throw GlobalException.build("参数错误(ERROR:NAME)");
        }
        TreatyPo treatyPoInit = new TreatyPo();
        treatyPoInit.setOrgId("00000");
        treatyPoInit.setTreatyName(treatyName);
        TreatyPo treatyInfo = treatyDao.getTreatyInfo(treatyPoInit);

        TreatyPo treatyPo = new TreatyPo();
        treatyPo.setUpdateTime(new Date());
        treatyPo.setTreatyContent(treatyInfo.getTreatyContent());
        treatyPo.setTreatyId(param.get("treatyId").toString());
        treatyPo.setOrgId(param.get("orgId").toString());
        treatyPo.setTreatyName(treatyName);
        treatyDao.edit(treatyPo);
    }

    public TreatyPo getTreatyInfo(Map<String, Object> param) {
        if(StringUtils.isEmpty(param.get("treatyName"))){
            throw GlobalException.build("参数错误");
        }
        String treatyName = param.get("treatyName").toString();
        String[] names = {"加盟代理规则","用户注册协议","供货商规则","联盟商家规则","旺旺直播规则","隐私政策"};
        List<String> asList = Arrays.asList(names);
        if(!asList.contains(treatyName)){
            throw GlobalException.build("参数错误(ERROR:NAME)");
        }
        TreatyPo treatyPo = new TreatyPo();
        String orgId = param.get("orgId").toString();
        treatyPo.setOrgId(orgId);
        treatyPo.setTreatyName(treatyName);
        TreatyPo treatyInfo = treatyDao.getTreatyInfo(treatyPo);
        //没查到就初始化
        if(null == treatyInfo){
            List<TreatyPo> treatyPos = this.initTreaty(orgId);
            for(TreatyPo trPo : treatyPos){
                if(treatyName.equals(trPo.getTreatyName())){
                    treatyInfo = trPo;
                }
            }
        }
        return treatyInfo;
    }

    public Map<String, Object> getReadRecord(Map<String, Object> param) {
        if(StringUtils.isEmpty(param.get("treatyName"))){
            throw GlobalException.build("参数错误");
        }
        String treatyName = param.get("treatyName").toString();
        String[] names = {"加盟代理规则","用户注册协议","供货商规则","联盟商家规则","旺旺直播规则","隐私政策"};
        List<String> asList = Arrays.asList(names);
        if(!asList.contains(treatyName)){
            throw GlobalException.build("参数错误(ERROR:NAME)");
        }
        Map<String, Object> readRecord = treatyDao.getReadRecord(param);
        if(null == readRecord){
            return new HashMap<>();
        }
        TreatyPo treatyInfo = this.getTreatyInfo(param);
        readRecord.put("updateTime",treatyInfo.getUpdateTime());
        return readRecord;
    }

    public void addReadRecord(Map<String, Object> param) {
        if(StringUtils.isEmpty(param.get("treatyName"))){
            throw GlobalException.build("参数错误");
        }
        String[] names = {"加盟代理规则","用户注册协议","供货商规则","联盟商家规则","旺旺直播规则","隐私政策"};
        if (param.get("treatyName") instanceof ArrayList) {
            ArrayList<String> tn = (ArrayList<String>)param.get("treatyName");
            for(String treatyName:tn){
                List<String> asList = Arrays.asList(names);
                if(!asList.contains(treatyName)){
                    throw GlobalException.build("参数错误(ERROR:NAME)");
                }
                param.put("treatyName",treatyName);
                treatyDao.addReadRecord(param);
            }
        }else{
            String treatyName = param.get("treatyName").toString();
            List<String> asList = Arrays.asList(names);
            if(!asList.contains(treatyName)){
                throw GlobalException.build("参数错误(ERROR:NAME)");
            }
            treatyDao.addReadRecord(param);
        }
    }
}
